// pages/register/register.ts

Page({
    redirectURL: '',
    data: {
        licNo: '',
        name: '',
        licImgURL: '',
        genders: ['未知', '男', '女'],
        genderIndex: 0,
        birthDate: '1990-01-01',
        state: 'UNSUBMITTED' as 'UNSUBMITTED' | 'PENDING' | 'VERIFIED'
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(opt: Record<'redirect', string>) {

        if (opt.redirect) {
            this.redirectURL = decodeURIComponent(opt.redirect)
        }

    },
    /**
     * 上传驾驶证
     */
    onUploadLic() {

        wx.chooseMedia({
            count: 9,
            mediaType: ['image', 'video'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success: res => {
                if (res.tempFiles.length > 0) {
                    this.setData({
                        licImgURL: res.tempFiles[0].tempFilePath
                    })
                }
                // TODO: upload image
                setTimeout(() => {
                    this.setData({
                        licNo: "黑A1530",
                        name: "张三",
                        genderIndex: 1,
                        birthDate: "1998-05-24"
                    })
                })
            }
        })

    },
    /**
     * 性别选择
     */
    onGenderChange(e: any) {
        this.setData({
            genderIndex: parseInt(e.detail.value)
        })
    },
    /**
     * 日期选择
     */
    onBirthDateChange(e: any) {
        this.setData({
            birthDate: e.detail.value,
        })
    },
    /**
     * 表单提交
     */
    onSubmit() {

        // TODO: submit for server
        this.setData({
            state: 'PENDING'
        })

        // TODO
        setTimeout(async () => {
            await this.onLicVerified()
        }, 3000)

    },
    /**
     * 重新表单提交
     */
    onReSubmit() {
        // TODO: submit for server
        this.setData({
            state: 'UNSUBMITTED',
            licImgURL: ''
        })
    },
    /**
     * 驾驶证审核
     */
    async onLicVerified() {

        this.setData({
            state: 'VERIFIED'
        })

        if (this.redirectURL) {
            // 跳转开锁页面
            await wx.redirectTo({
                url: this.redirectURL,
            })
        }


    }

})
